package cn.framelife.rainbow.util.xml;

import java.util.List;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * Spring配置文件
 * 
 * @author 潘广伟 p_3er@qq.com  
 * @date 2014-1-13 上午11:06:19
 */
public class SpringXMLUtil extends XMLUtil{
	/**
	 * 更新实体类的Spring配置信息
	 *
	 * @param springPath
	 * @param annotatedClasses
	 * @throws Exception
	 */
	public static void updateAnnotatedClasses(String springPath,List<String> annotatedClasses) throws Exception{
		Document springDoc = getDocument(springPath);
		
		Element listNode = (Element) selectSingleNode("/beans/bean/property[@name='annotatedClasses']/list", springDoc.getDocumentElement());
		
		/**
		 * 先移除已有的实体类
		 */
		NodeList list = listNode.getChildNodes();
		for (int i = 0; i < list.getLength(); i++) {
			Node node = list.item(i);
			String name = node.getTextContent();
			if(annotatedClasses.contains(name)){
				annotatedClasses.remove(name);
			}
		}
		
		Element elem = null;
		for(String annotatedClass : annotatedClasses){
			elem = springDoc.createElement("value");
			elem.setTextContent(annotatedClass);
			listNode.appendChild(elem);
		}
		
		saveXml(springPath, springDoc);
	}

}
